package com.horizon.manage.classes.converter;

import com.horizon.common.constrants.HorizonConstants;
import com.horizon.common.converter.HorizonBaseEvdConverter;
import com.horizon.common.util.HorizonUtil;
import com.horizon.manage.classes.dto.ClassesDTO;
import com.horizon.manage.classes.entity.Classes;
import com.horizon.manage.classes.vo.ClassesVO;
import org.springblade.core.mp.support.BaseEntityWrapper;
import org.springframework.stereotype.Component;

import java.time.LocalDate;
import java.time.LocalDateTime;
import java.time.Month;

/**
 * 班级实体、视图、DTO转换器.
 *
 * @author zhaoshb
 * @since 2022/12/8 22:57
 */
@Component
public class ClassesEvdConverter extends HorizonBaseEvdConverter<Classes, ClassesVO, ClassesDTO> {

    @Override
    public Classes dtoToEntity(ClassesDTO dto) {
        Classes classes = new Classes();
        classes.setId(dto.getId());
        classes.setYears(dto.getYears());
        classes.setNumber(dto.getNumber());
        return classes;
    }

    @Override
    public ClassesVO entityToVO(Classes entity) {
        ClassesVO vo = new ClassesVO();
        vo.setId(entity.getId());
        vo.setYears(entity.getYears());
        vo.setNumber(entity.getNumber());
        vo.setShowNameA(getShowNameA(entity.getYears(), entity.getNumber()));
        vo.setShowNameB(getShowNameB(entity.getYears(), entity.getNumber()));
        return vo;
    }

    private String getShowNameA(Integer years, Integer number) {

        return HorizonUtil.getLocaleMessage(HorizonConstants.LC_CODE_CLASSES_SHOW_NAME_A, new Object[]{years.toString(), number.toString()});
    }

    private String getShowNameB(Integer years, Integer number) {
        return HorizonUtil.getLocaleMessage(HorizonConstants.LC_CODE_CLASSES_SHOW_NAME_B, new Object[]{Integer.toString(getShowNameBYears(years)), number.toString()});
    }

    private int getShowNameBYears(Integer years) {
        LocalDateTime now = LocalDateTime.now();
        int year = now.getYear();
        int monthValue = now.getMonthValue();
        if (year == years) {
            return 1;
        }
        if (monthValue < 9) {
            return year - years;
        }
        return year - years + 1;
    }
}
